<template>
  <div class="analytics-page">
    <div class="page-header">
      <h1 class="page-title">数据统计</h1>
      <p class="page-subtitle">文档使用情况和系统统计分析</p>
    </div>

    <div class="analytics-content">
      <!-- 统计概览 -->
      <div class="stats-overview">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-card class="stat-card">
              <div class="stat-content">
                <div class="stat-icon primary">
                  <el-icon><DataLine /></el-icon>
                </div>
                <div class="stat-info">
                  <div class="stat-value">{{ documentStore.totalDocuments }}</div>
                  <div class="stat-label">总文档数</div>
                </div>
              </div>
            </el-card>
          </el-col>
          
          <el-col :span="6">
            <el-card class="stat-card">
              <div class="stat-content">
                <div class="stat-icon success">
                  <el-icon><FolderOpened /></el-icon>
                </div>
                <div class="stat-info">
                  <div class="stat-value">{{ projectStats.active }}</div>
                  <div class="stat-label">活跃项目</div>
                </div>
              </div>
            </el-card>
          </el-col>
          
          <el-col :span="6">
            <el-card class="stat-card">
              <div class="stat-content">
                <div class="stat-icon warning">
                  <el-icon><User /></el-icon>
                </div>
                <div class="stat-info">
                  <div class="stat-value">{{ projectStats.totalMembers }}</div>
                  <div class="stat-label">团队成员</div>
                </div>
              </div>
            </el-card>
          </el-col>
          
          <el-col :span="6">
            <el-card class="stat-card">
              <div class="stat-content">
                <div class="stat-icon info">
                  <el-icon><View /></el-icon>
                </div>
                <div class="stat-info">
                  <div class="stat-value">{{ totalUsage }}</div>
                  <div class="stat-label">总访问次数</div>
                </div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>

      <!-- 图表展示区域 -->
      <el-row :gutter="20" class="charts-section">
        <el-col :span="12">
          <el-card class="chart-card">
            <template #header>
              <div class="chart-header">
                <h3>文档类型分布</h3>
                <el-icon><PieChart /></el-icon>
              </div>
            </template>
            <div class="chart-placeholder">
              <div class="chart-icon">
                <el-icon><PieChart /></el-icon>
              </div>
              <h4>文档类型统计</h4>
              <div class="chart-data">
                <div class="data-item">
                  <span class="data-label">PDF:</span>
                  <span class="data-value">{{ documentStats.pdf }} 个</span>
                </div>
                <div class="data-item">
                  <span class="data-label">Word:</span>
                  <span class="data-value">{{ documentStats.doc }} 个</span>
                </div>
                <div class="data-item">
                  <span class="data-label">PPT:</span>
                  <span class="data-value">{{ documentStats.ppt }} 个</span>
                </div>
                <div class="data-item">
                  <span class="data-label">图片:</span>
                  <span class="data-value">{{ documentStats.img }} 个</span>
                </div>
              </div>
            </div>
          </el-card>
        </el-col>
        
        <el-col :span="12">
          <el-card class="chart-card">
            <template #header>
              <div class="chart-header">
                <h3>项目进度概览</h3>
                <el-icon><DataLine /></el-icon>
              </div>
            </template>
            <div class="chart-placeholder">
              <div class="chart-icon">
                <el-icon><DataLine /></el-icon>
              </div>
              <h4>项目完成情况</h4>
              <div class="progress-list">
                <div
                  v-for="project in projectStore.projects"
                  :key="project.id"
                  class="progress-item"
                >
                  <div class="progress-info">
                    <span class="project-name">{{ project.name }}</span>
                    <span class="progress-text">{{ project.progress }}%</span>
                  </div>
                  <el-progress 
                    :percentage="project.progress" 
                    :stroke-width="8"
                    :show-text="false"
                    :color="project.color"
                  />
                </div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <!-- 热门文档排行 -->
      <el-card class="ranking-card">
        <template #header>
          <div class="chart-header">
            <h3>热门文档排行</h3>
            <el-icon><TrendCharts /></el-icon>
          </div>
        </template>
        
        <div class="ranking-list">
          <div
            v-for="(doc, index) in topDocuments"
            :key="doc.id"
            class="ranking-item"
            @click="previewDocument(doc)"
          >
            <div class="ranking-number" :class="getRankClass(index)">
              {{ index + 1 }}
            </div>
            
            <div class="document-icon" :class="doc.type">
              <el-icon>
                <Document v-if="doc.type === 'pdf'" />
                <Reading v-else-if="doc.type === 'doc'" />
                <Present v-else-if="doc.type === 'ppt'" />
                <Picture v-else />
              </el-icon>
            </div>
            
            <div class="document-info">
              <div class="document-title">{{ doc.title }}</div>
              <div class="document-meta">
                <el-tag size="small" :color="getProjectColor(doc.project)">
                  {{ doc.project }}
                </el-tag>
                <span class="meta-item">{{ doc.size }}</span>
              </div>
            </div>
            
            <div class="usage-stats">
              <div class="usage-count">{{ doc.usageCount }}</div>
              <div class="usage-label">次访问</div>
            </div>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useDocumentStore } from '@/store/document'
import { useProjectStore } from '@/store/project'
import {
  DataLine, FolderOpened, User, View, PieChart, TrendCharts,
  Document, Reading, Present, Picture
} from '@element-plus/icons-vue'

const documentStore = useDocumentStore()
const projectStore = useProjectStore()

// 计算属性
const documentStats = computed(() => documentStore.documentStats)

const projectStats = computed(() => {
  const stats = projectStore.getProjectStats()
  return stats
})

const totalUsage = computed(() => {
  return documentStore.documents.reduce((total, doc) => total + doc.usageCount, 0)
})

const topDocuments = computed(() => {
  return [...documentStore.documents]
    .sort((a, b) => b.usageCount - a.usageCount)
    .slice(0, 10)
})

// 方法
const getRankClass = (index) => {
  if (index === 0) return 'gold'
  if (index === 1) return 'silver'
  if (index === 2) return 'bronze'
  return 'normal'
}

const getProjectColor = (projectName) => {
  const project = projectStore.projects.find(p => p.name === projectName)
  return project ? project.color : '#909399'
}

const previewDocument = (doc) => {
  // 这里可以调用预览功能
  console.log('预览文档:', doc.title)
}
</script>

<style lang="scss" scoped>
.analytics-page {
  padding: 20px;
}

.page-header {
  margin-bottom: 24px;
  text-align: center;
  
  .page-title {
    font-size: 28px;
    font-weight: 600;
    color: var(--el-text-color-primary);
    margin-bottom: 8px;
  }
  
  .page-subtitle {
    color: var(--el-text-color-secondary);
    font-size: 14px;
  }
}

.stats-overview {
  margin-bottom: 24px;
  
  .stat-card {
    height: 120px;
    
    :deep(.el-card__body) {
      height: 100%;
      padding: 20px;
    }
    
    .stat-content {
      display: flex;
      align-items: center;
      height: 100%;
      gap: 16px;
      
      .stat-icon {
        width: 64px;
        height: 64px;
        border-radius: 16px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 28px;
        color: white;
        
        &.primary { background: var(--gradient-primary); }
        &.success { background: var(--gradient-success); }
        &.warning { background: var(--gradient-warning); }
        &.info { background: var(--gradient-danger); }
      }
      
      .stat-info {
        .stat-value {
          font-size: 32px;
          font-weight: 700;
          color: var(--el-text-color-primary);
          margin-bottom: 4px;
        }
        
        .stat-label {
          font-size: 14px;
          color: var(--el-text-color-secondary);
        }
      }
    }
  }
}

.charts-section {
  margin-bottom: 24px;
}

.chart-card, .ranking-card {
  .chart-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    
    h3 {
      margin: 0;
      font-size: 16px;
      font-weight: 600;
      color: var(--el-text-color-primary);
    }
    
    .el-icon {
      font-size: 18px;
      color: var(--el-text-color-secondary);
    }
  }
  
  .chart-placeholder {
    text-align: center;
    padding: 40px 20px;
    
    .chart-icon {
      font-size: 64px;
      color: var(--el-text-color-placeholder);
      margin-bottom: 16px;
    }
    
    h4 {
      font-size: 18px;
      color: var(--el-text-color-primary);
      margin-bottom: 20px;
    }
    
    .chart-data {
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      gap: 12px;
      max-width: 200px;
      margin: 0 auto;
      
      .data-item {
        display: flex;
        justify-content: space-between;
        padding: 8px 12px;
        background: var(--el-fill-color-lighter);
        border-radius: 6px;
        
        .data-label {
          font-size: 14px;
          color: var(--el-text-color-secondary);
        }
        
        .data-value {
          font-size: 14px;
          font-weight: 600;
          color: var(--el-text-color-primary);
        }
      }
    }
    
    .progress-list {
      .progress-item {
        margin-bottom: 16px;
        
        &:last-child {
          margin-bottom: 0;
        }
        
        .progress-info {
          display: flex;
          justify-content: space-between;
          align-items: center;
          margin-bottom: 8px;
          
          .project-name {
            font-size: 14px;
            color: var(--el-text-color-primary);
          }
          
          .progress-text {
            font-size: 14px;
            font-weight: 600;
            color: var(--el-color-primary);
          }
        }
      }
    }
  }
}

.ranking-list {
  .ranking-item {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 16px 0;
    border-bottom: 1px solid var(--el-border-color-lighter);
    cursor: pointer;
    transition: background-color 0.2s;
    
    &:hover {
      background: var(--el-fill-color-lighter);
      margin: 0 -16px;
      padding-left: 16px;
      padding-right: 16px;
      border-radius: 8px;
    }
    
    &:last-child {
      border-bottom: none;
    }
    
    .ranking-number {
      width: 32px;
      height: 32px;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 16px;
      font-weight: 600;
      color: white;
      flex-shrink: 0;
      
      &.gold { background: linear-gradient(135deg, #FFD700, #FFA500); }
      &.silver { background: linear-gradient(135deg, #C0C0C0, #A9A9A9); }
      &.bronze { background: linear-gradient(135deg, #CD7F32, #B8860B); }
      &.normal { background: var(--el-color-info); }
    }
    
    .document-icon {
      width: 40px;
      height: 40px;
      border-radius: 8px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 18px;
      color: white;
      flex-shrink: 0;
      
      &.pdf { background: var(--gradient-danger); }
      &.doc { background: var(--gradient-primary); }
      &.ppt { background: var(--gradient-warning); }
      &.img { background: var(--gradient-success); }
    }
    
    .document-info {
      flex: 1;
      min-width: 0;
      
        .document-title {
        font-size: 16px;
        font-weight: 500;
        color: var(--el-text-color-primary);
        margin-bottom: 8px;
          @extend .text-ellipsis !optional;
      }
      
      .document-meta {
        display: flex;
        gap: 12px;
        align-items: center;
        
        .meta-item {
          font-size: 14px;
          color: var(--el-text-color-secondary);
        }
      }
    }
    
    .usage-stats {
      text-align: center;
      
      .usage-count {
        font-size: 24px;
        font-weight: 600;
        color: var(--el-color-primary);
        margin-bottom: 4px;
      }
      
      .usage-label {
        font-size: 12px;
        color: var(--el-text-color-secondary);
      }
    }
  }
}
</style>